import openpyxl

import macropodus
import pandas as pd
import numpy as np
from MyPackages.UnionFindSet import UnionFindSet


# sent1 = "化学药品原料药制造"
# sent2 = "化学药品制剂制造"
#
# # 文本相似度(similarity)
# sents = macropodus.sim(sent1, sent2, type_sim="total", type_encode="avg")
# print(sents)
# sents = macropodus.sim(sent1, sent2, type_sim="cosine", type_encode="single")
# print(sents)


def check(s1, s2, thred):
    sents = macropodus.sim(s1, s2, type_sim="total", type_encode="avg")
    if sents > thred:
        return True
    else:
        return False


# 输入一列数据(Series类型)，判断两两元素之间是否相似，返回一个元素为二元组的列表
def similar_list(data, thred):
    length = data.notnull().sum()
    slit = []
    for i in range(length):
        for j in range(i + 1, length):
            if check(data[i], data[j], thred):
                s = (i, j)
                slit.append(s)

    return slit


def load_data(sheet_name):
    # df = pd.read_excel('dataset/标签测试.xlsx', sheet_name=sheet_name)
    df = pd.read_excel('result/newresult3.xlsx', sheet_name=sheet_name)
    return df



def Main(context):
    df = load_data(context['yiji'])
    data = df[context['erji']]
    sl = similar_list(data, context['thred'])
    uf = UnionFindSet(data, sl)
    re = uf.excute()
    max = 0.9
    mostSim = ''
    for i in range(len(re)-1):
        sent = macropodus.sim(re[i][0], context['searchWord'], type_sim="total", type_encode="avg")
        if sent > max:
            max = sent
            mostSim = re[i]
    if mostSim == '':
        print('没有发现近义词')
    else:
        print(mostSim)


# Press the green button in the gutter to run the script.
if __name__ == '__main__':

    # yiji = input("请输入一级标签的名称")
    # erji = input("请输入二级标签的名称")
    # searchWord = input("请输入要查询的主题词")
    #context = {'yiji': yiji, 'erji': erji, 'searchWord': searchWord, 'thred': 0.9}
    context = {'yiji': '政策企业标签', 'erji': '政策提及的企业规模', 'searchWord': '小型企业', 'thred': 0.9}
    Main(context)
